package com.ruoyi.web.controller.biz;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.valid.ValidInsertGroup;
import com.ruoyi.common.valid.ValidUpdateGroup;
import com.ruoyi.system.domain.BsQiye;
import com.ruoyi.system.domain.BsQiyeMerchandise;
import com.ruoyi.system.domain.excel.BsCulturalAndCreativeExcel;
import com.ruoyi.system.domain.excel.BsQiyeMerchandiseExcel;
import com.ruoyi.system.domain.ro.BsQiyeMerchandiseRO;
import com.ruoyi.system.domain.ro.BsQiyeRo;
import com.ruoyi.system.service.IBsQiyeMerchandiseService;
import com.ruoyi.system.service.IBsQiyeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * 企业经营数据
 *
 * @author ruoyi
 * @date 2023-11-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@Api(tags = "WEB端-文创数据")
@RequestMapping("/bs/culturalAndCreative")
public class BsCulturalAndCreativeController extends BaseController {

    private final IBsQiyeService iBsQiyeService;
    private final IBsQiyeMerchandiseService iBsQiyeMerchandiseService;

    @ApiOperation(value = "文创数据-列表导出", tags = {"WEB端-文创数据"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "商店类型 详见字典merchant_type"),
            @ApiImplicitParam(name = "jingquId", value = "景区id"),
            @ApiImplicitParam(name = "applyName", value = "申请人"),
            @ApiImplicitParam(name = "name", value = "商铺名称"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态 0 已认证/待审核 1审核通过 2审核拒绝 详见字典merchant_type"),
            @ApiImplicitParam(name = "beginCreateTime", value = "申请时间-start"),
            @ApiImplicitParam(name = "endCreateTime", value = "申请时间-end"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/export")
    public AjaxResult<?> export(@RequestParam(required = false) String type,
                                @RequestParam(required = false) Long jingquId, @RequestParam(required = false) String applyName,
                                @RequestParam(required = false) String name, @Pattern(regexp = "^(0)$|^(1)$|^(2)$|^$", message = "auditStatus不合法") @RequestParam(required = false) String auditStatus,
                                @RequestParam(required = false) String beginCreateTime, @RequestParam(required = false) String endCreateTime) {
        startPage();
        BsQiyeRo ro = BsQiyeRo.builder().type(type).jingquId(jingquId).applyName(applyName).name(name).auditStatus(auditStatus).build();
        HashMap<String, Object> param = new HashMap<>(4);
        param.put("beginCreateTime", beginCreateTime);
        param.put("endCreateTime", endCreateTime);
        ro.setParams(param);
        List<BsQiye> list = iBsQiyeService.queryList(ro, true);
        ExcelUtil<BsCulturalAndCreativeExcel> util = new ExcelUtil<>(BsCulturalAndCreativeExcel.class);
        return util.exportExcel(ConvertUtil.sourceToTarget(list, BsCulturalAndCreativeExcel.class), "文创数据");
    }

    @ApiOperation(value = "文创数据-列表", tags = {"WEB端-文创数据"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "商店类型 详见字典merchant_type"),
            @ApiImplicitParam(name = "jingquId", value = "景区id"),
            @ApiImplicitParam(name = "applyName", value = "申请人"),
            @ApiImplicitParam(name = "name", value = "商铺名称"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态 0 已认证/待审核 1审核通过 2审核拒绝 详见字典merchant_type"),
            @ApiImplicitParam(name = "beginCreateTime", value = "申请时间-start"),
            @ApiImplicitParam(name = "endCreateTime", value = "申请时间-end"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/list")
    public Result<TableDataInfo<BsQiye>> enterList(@RequestParam(required = false) String type,
                                                   @RequestParam(required = false) Long jingquId, @RequestParam(required = false) String applyName,
                                                   @RequestParam(required = false) String name, @Pattern(regexp = "^(0)$|^(1)$|^(2)$|^$", message = "auditStatus不合法") @RequestParam(required = false) String auditStatus,
                                                   @RequestParam(required = false) String beginCreateTime, @RequestParam(required = false) String endCreateTime) {
        startPage();
        BsQiyeRo ro = BsQiyeRo.builder().type(type).jingquId(jingquId).applyName(applyName).name(name).auditStatus(auditStatus).build();
        HashMap<String, Object> param = new HashMap<>(4);
        param.put("beginCreateTime", beginCreateTime);
        param.put("endCreateTime", endCreateTime);
        ro.setParams(param);
        List<BsQiye> list = iBsQiyeService.queryList(ro, true);
        return Result.success(getDataTable(list));
    }

    //商品列表
    @ApiOperation(value = "商品种类-列表", tags = {"WEB端-文创数据"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qiYeId", value = "店铺id"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/merchandiseList")
    public Result<TableDataInfo<BsQiyeMerchandise>> enterList(@RequestParam() String qiYeId) {
        startPage();
        List<BsQiyeMerchandise> list = iBsQiyeMerchandiseService.queryList(Long.parseLong(qiYeId));
        return Result.success(getDataTable(list));
    }

    //商品列表
    @ApiOperation(value = "商品种类-列表导出", tags = {"WEB端-文创数据"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qiYeId", value = "店铺id"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/merchandiseListExport")
    public AjaxResult<?> merchandiseListExport(@RequestParam() String qiYeId) {
        startPage();
        List<BsQiyeMerchandise> list = iBsQiyeMerchandiseService.queryList(Long.parseLong(qiYeId));
        ExcelUtil<BsQiyeMerchandiseExcel> util = new ExcelUtil<>(BsQiyeMerchandiseExcel.class);
        return util.exportExcel(ConvertUtil.sourceToTarget(list, BsQiyeMerchandiseExcel.class), "商品种类列表导出");
    }


    //商品新增
    @ApiOperation(value = "商品-新增", tags = "WEB端-文创数据")
    @Log(title = "文创数据-商品-新增", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/merchandiseAdd")
    public Result<?> add(@Validated(ValidInsertGroup.class) @RequestBody BsQiyeMerchandiseRO ro) {
        return Result.toResult(iBsQiyeMerchandiseService.insertByRO(ro));
    }

    //商品修改
    @ApiOperation(value = "商品-修改", tags = "WEB端-文创数据")
    @Log(title = "文创数据-商品-修改", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/merchandiseEdit")
    public Result<?> edit(@Validated(ValidUpdateGroup.class) @RequestBody BsQiyeMerchandiseRO ro) {
        return Result.toResult(iBsQiyeMerchandiseService.updateByRO(ro));
    }

    //商品删除
    @ApiOperation(value = "商品-删除", tags = "WEB端-文创数据")
    @ApiImplicitParam(name = "id", value = "商品id")
    @Log(title = "文创数据-商品-删除", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @GetMapping("/merchandiseDetele")
    public Result<?> remove(@RequestParam Long id) {
        return Result.toResult(iBsQiyeMerchandiseService.deleteWithValidByIds(Collections.singletonList(id)));
    }

    //商品删除
    @ApiOperation(value = "商品-详情", tags = {"WEB端-文创数据", "游客端-文创数据"})
    @ApiImplicitParam(name = "id", value = "商品id")
    @GetMapping("/detail")
    public Result<?> detail(@RequestParam Long id) {
        return Result.success(iBsQiyeMerchandiseService.detail(id));
    }


}
